<?php

namespace App\Repositories;


use App\Exceptions\BasicException;
use App\Services\Tools\CommonService;
use App\Services\Tools\TimeService;
use Illuminate\Support\Facades\DB;
use App\Enums\ErmEnum;

//引入hash

class ErmIndexRepository
{

    /*预警系统首页*/
    public function getIndexData()
    {
        $today_res = TimeService::today();
        $yesterday_res = TimeService::yesterday();
        $week_res = TimeService::week();
        /*今日加入灰名单用户数量*/
        $join_gray['today_num'] = app("model_erm_list_record")->where(['new_type' => 3])->whereBetween('create_time', $today_res)->count();
        $yesterday_num = app("model_erm_list_record")->where(['new_type' => 3])->whereBetween('create_time', $yesterday_res)->count();
        $join_gray['difference_num'] = abs($join_gray['today_num'] - $yesterday_num);
        $join_gray['difference_type'] = $join_gray['today_num'] - $yesterday_num >= 0 ? 1 : 2;
        $join_gray['week_num'] = app("model_erm_list_record")->where(['new_type' => 3])->whereBetween('create_time', $week_res)->count();
        /*今日脱离灰名单用户数量*/
        $remove_gray['today_num'] = app("model_erm_list_record")->where(['old_type' => 3])->whereBetween('create_time', $today_res)->count();
        $yesterday_num = app("model_erm_list_record")->where(['old_type' => 3])->whereBetween('create_time', $yesterday_res)->count();
        $remove_gray['difference_num'] = abs($remove_gray['today_num'] - $yesterday_num);
        $remove_gray['difference_type'] = $remove_gray['today_num'] - $yesterday_num >= 0 ? 1 : 2;
        $remove_gray['week_num'] = app("model_erm_list_record")->where(['old_type' => 3])->whereBetween('create_time', $week_res)->count();
        /*今日加入黑名单用户数量*/
        $join_black['today_num'] = app("model_erm_list_record")->where(['new_type' => 4])->whereBetween('create_time', $today_res)->count();
        $yesterday_num = app("model_erm_list_record")->where(['new_type' => 4])->whereBetween('create_time', $yesterday_res)->count();
        $join_black['difference_num'] = abs($join_black['today_num'] - $yesterday_num);
        $join_black['difference_type'] = $join_black['today_num'] - $yesterday_num >= 0 ? 1 : 2;
        $join_black['week_num'] = app("model_erm_list_record")->where(['new_type' => 4])->whereBetween('create_time', $week_res)->count();
        /*今日脱离黑名单用户数量*/
        $remove_black['today_num'] = app("model_erm_list_record")->where(['old_type' => 4])->whereBetween('create_time', $today_res)->count();
        $yesterday_num = app("model_erm_list_record")->where(['old_type' => 4])->whereBetween('create_time', $yesterday_res)->count();
        $remove_black['difference_num'] = abs($remove_black['today_num'] - $yesterday_num);
        $remove_black['difference_type'] = $remove_black['today_num'] - $yesterday_num >= 0 ? 1 : 2;
        $remove_black['week_num'] = app("model_erm_list_record")->where(['old_type' => 4])->whereBetween('create_time', $week_res)->count();

        /*现存灰/黑名单用户*/
        $existing['gray_num'] = app("model_erm_public_list")->where(['type' => 3])->count();
        /*灰名单一项实名*/
        $ermGrayListModel = app('model_erm_gray_list');
        $ermAuthenticationRecordModel = app('model_erm_authentication_record');
        $existing['gray_num_one'] = $ermGrayListModel
            ->selectRaw('count(t1.user_id) as num')
            ->from($ermGrayListModel->getTable() . " as t1")
            ->Join(DB::raw($ermAuthenticationRecordModel->getTable() . " as t2"), "t1.user_id", "=", "t2.user_id")
            ->where(['t2.status' => 1, 't2.is_effect' => 0, 't2.detail_type' => ErmEnum::AUTO_REMOVE_GRAY_LIST])
            ->groupBy('t1.user_id')
            ->having('num', '=', 1)
            ->get()->toArray();
        $existing['gray_num_one'] = count($existing['gray_num_one']);
        /*灰名单二项实名*/
        $existing['gray_num_two'] = $ermGrayListModel
            ->selectRaw('count(t1.user_id) as num')
            ->from($ermGrayListModel->getTable() . " as t1")
            ->Join(DB::raw($ermAuthenticationRecordModel->getTable() . " as t2"), "t1.user_id", "=", "t2.user_id")
            ->where(['t2.status' => 1, 't2.is_effect' => 0, 't2.detail_type' => ErmEnum::AUTO_REMOVE_GRAY_LIST])
            ->groupBy('t1.user_id')
            ->having('num', '=', 2)
            ->get()->toArray();
        $existing['gray_num_two'] = count($existing['gray_num_two']);
        /*灰名单三项实名*/
        $existing['gray_num_three'] = $ermGrayListModel
            ->selectRaw('count(t1.user_id) as num')
            ->from($ermGrayListModel->getTable() . " as t1")
            ->Join(DB::raw($ermAuthenticationRecordModel->getTable() . " as t2"), "t1.user_id", "=", "t2.user_id")
            ->where(['t2.status' => 1, 't2.is_effect' => 0, 't2.detail_type' => ErmEnum::AUTO_REMOVE_GRAY_LIST])
            ->groupBy('t1.user_id')
            ->having('num', '=', 3)
            ->get()->toArray();
        $existing['gray_num_three'] = count($existing['gray_num_three']);
        $existing['black_num'] = app("model_erm_public_list")->where(['type' => 4])->count();
        $all_num = $existing['gray_num'] + $existing['gray_num_one'] + $existing['gray_num_two'] + $existing['gray_num_three'] + $existing['black_num'];
        $existing['gray_percentage'] = $all_num > 0 ? round($existing['gray_num'] / $all_num * 100) : 0;
        $existing['gray_percentage_one'] = $all_num > 0 ? round($existing['gray_num_one'] / $all_num * 100) : 0;
        $existing['gray_percentage_two'] = $all_num > 0 ? round($existing['gray_num_two'] / $all_num * 100) : 0;
        $existing['gray_percentage_three'] = $all_num > 0 ? round($existing['gray_num_three'] / $all_num * 100) : 0;
        $existing['black_percentage'] = $all_num > 0 ? round($existing['black_num'] / $all_num * 100) : 0;

        return [
            'join_gray' => $join_gray,
            'remove_gray' => $remove_gray,
            'join_black' => $join_black,
            'remove_black' => $remove_black,
            'existing' => $existing,
        ];
    }


}
